use std::io::Write;
use core::str::from_utf8;
use std::io::{Error, Read};
use std::net::{TcpListener,TcpStream};
fn handle_client(mut stream: TcpStream) -> Result<(), Error>{
    // 定义512 字节 buffer 数组 并且初始化成0
    let mut buf = [0;512];
    loop{
        // 读取内容
        let len = stream.read(&mut buf).unwrap();
        if len == 0 {
            break;
        }
        println!("read {} bytes {:?}", len, from_utf8(&buf[..len]));
    }
    Ok(())
}
fn main() {
    // 创建TCP服务器
    let listener = TcpListener::bind("127.0.0.1:9999").expect("创建失败！");
    // 接收客户端消息
    for stream in listener.incoming() {
        // 异步线程处理消息
        std::thread::spawn(move || {
            handle_client(stream.unwrap()).expect("处理失败！");
        });
    }
}
